#include "connection_db.h"

QSqlDatabase Connection_Db::getDatabase()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setPort(3306);
    db.setDatabaseName("baiduwangpan.db");
    db.setUserName("root");
    db.setPassword("123456");
    db.open();
    if(!db.open())
    {
        qDebug()<<"不能连接"<<db.lastError().text();
    }
    else
    {
        qDebug()<<"连接成功";
    }
    return db;
}

QSqlQuery Connection_Db::statement(QString sql)
{
    QSqlDatabase db = getDatabase();
    QSqlQuery query;
    if(!query.exec(sql)){
        qDebug()<<"query from home_work_cpp error";
        qDebug()<<query.lastError();
    }
    db.close();
    return query;
}
